package com.gxa.config;

import com.gxa.modules.wechatminiprogram.authentication.XcxLoginAuthenticationManager;
import com.gxa.modules.wechatminiprogram.filter.XcxLoginAbstractAuthenticationProcessingFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

/**
 * Filter配置
 *
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean shiroFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new DelegatingFilterProxy("shiroFilter"));
        //该值缺省为false，表示生命周期由SpringApplicationContext管理，设置为true则表示由ServletContainer管理
        registration.addInitParameter("targetFilterLifecycle", "true");
        registration.setEnabled(true);
        registration.setOrder(Integer.MAX_VALUE - 1);
        registration.addUrlPatterns("/*");
        return registration;
    }

    /**
     * 小程序 登录 认证处理过滤器 注册为 Bean
     *
     * @return 可使用 Autowired 的 小程序 登录 认证处理过滤器
     */
    @Bean
    XcxLoginAbstractAuthenticationProcessingFilter xcxLoginAbstractAuthenticationProcessingFilterBean() {
        XcxLoginAbstractAuthenticationProcessingFilter xcxLoginFilter = new XcxLoginAbstractAuthenticationProcessingFilter("/xcx/login.do");
        xcxLoginFilter.setAuthenticationManager(new XcxLoginAuthenticationManager());
        return xcxLoginFilter;
    }

    /**
     * 小程序 登录 认证处理过滤器
     */
    @Bean
    public FilterRegistrationBean smsAbstractAuthenticationProcessingFilter() {
        FilterRegistrationBean<XcxLoginAbstractAuthenticationProcessingFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(xcxLoginAbstractAuthenticationProcessingFilterBean());
        return registration;
    }

}